package com.hzb.gulimall.product.app;

import com.hzb.gulimall.product.entity.UidType;
import com.hzb.gulimall.product.service.IUidType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

@RestController
@RequestMapping("uid/info")
public class UidTypeController {

    @Resource
    private IUidType uidTypeService;

    /**
     * 202453
     */
    @RequestMapping("/save")
    public void save(){
        //ArrayList<UidType> uidTypes1 = toArrayByFileReader("F:\\对照文档.txt");
        ArrayList<UidType> uidTypes = toArrayByFileReader1("F:\\20210322.txt");

        /*ArrayList<UidType> uidTypeArrayList = new ArrayList<>();
        for (UidType type : uidTypes1) {
            for (UidType uidType : uidTypes) {
                if (type.getType().equals(uidType.getUid()+"_"+uidType.getType())){
                    uidTypeArrayList.add(type);
                    System.out.println(type.getAccount()+"-----");
                }
            }
        }*/
        uidTypeService.saveBatch(uidTypes);
        System.out.println("保存成功");
    }

    private ArrayList<UidType> toArrayByFileReader(String name) {
        ArrayList<UidType> uidTypes = new ArrayList<>();
        // 使用ArrayList来存储每行读取到的字符串
        ArrayList<String> arrayList = new ArrayList<>();
        try {
            FileReader fr = new FileReader(name);
            BufferedReader bf = new BufferedReader(fr);
            String str;
            // 按行读取字符串
            while ((str = bf.readLine()) != null) {
                arrayList.add(str);
            }
            bf.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对ArrayList中存储的字符串进行处理
        int length = arrayList.size();
        int[] array = new int[length];
        for (int i = 0; i < length; i++) {
            UidType uidType = new UidType();

            String s = arrayList.get(i);
            String[] s2 = s.split(",");
            uidType.setType(s2[3]);
            uidType.setAccount(s2[1]);
            uidTypes.add(uidType);
        }
        // 返回数组
        return uidTypes;
    }

    public ArrayList<UidType> toArrayByFileReader1(String name) {
        // 使用ArrayList来存储每行读取到的字符串
        ArrayList<String> arrayList = new ArrayList<>();
        try {
            FileReader fr = new FileReader(name);
            BufferedReader bf = new BufferedReader(fr);
            String str;
            // 按行读取字符串
            while ((str = bf.readLine()) != null) {
                arrayList.add(str);
            }
            bf.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ArrayList<UidType> uidTypes = new ArrayList<>();
        // 对ArrayList中存储的字符串进行处理
        int length = arrayList.size();
        int[] array = new int[length];
        for (int i = 0; i < length; i++) {
            UidType uidType = new UidType();
            String s = arrayList.get(i);
            //String[] s1 = s.split("@");
            //uidType.setUid(s1[0]);
            //uidType.setType(s1[1]);
            uidType.setType(s);
            uidType.setUid("11");
            uidTypes.add(uidType);
        }
        // 返回数组
        return uidTypes;
    }
}
